import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index.js'
import login from '../views/login.vue'
import manage from '../views/manage.vue'
import per from '../components/per.vue'
import chart from '../components/chart.vue'
import dataList from '../components/dataList.vue'
import home from '../components/home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', name: 'login', component: login },
  { path: '/manage', redirect:'/manage/home' },
  {
    path: '/manage', name: 'Manage', component: manage, meta: { requiresAuth: true },
    children: [
      { path: 'home', name: 'home', component: home, meta: { requiresAuth: true } },
      { path: 'datalist', name: 'dataList', component: dataList, meta: { requiresAuth: true } },
      { path: 'chart', name: 'Chart', component: chart, meta: { requiresAuth: true } },
      { path: 'per', name: 'Per', component: per, meta: { superAuth: true } },
    ]
  },
]



const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.requiresAuth){ //登录权限
    if (store.state.token && store.state.past === false) {  // 通过vuex state获取当前的token是否存在
      next();
    }else{
      next({
        path: '/'
      })
    }
  }else if(to.meta.superAuth){ //超级权限
    if (store.state.power == -1&&store.state.token && store.state.past === false) {  // 通过vuex state获取当前的token是否存在
      next();
    }else{
      next({
        path: '/manage'
      })
    }
  }else{
    next();
  }
})
export default router
